#!/usr/bin/python

import pyglet
from pyglet.gl import *
import MainMenu
import Editor
import Game
import glgui2
import Plan
      
screen = (1024,768)
try:
    # Try and create a window with multisampling (antialiasing)
    config = Config(sample_buffers=0, samples=0, 
                    depth_size=16, double_buffer=True,)
    window = pyglet.window.Window(resizable=False, config=config,width = screen[0],height = screen[1])
except pyglet.window.NoSuchConfigException:
    # Fall back to no multisampling for old hardware
    window = pyglet.window.Window(resizable=True)

@window.event
def on_resize(width, height):
    # Override the default on_resize handler to create a 3D projection
    glViewport(0, 0, screen[0],screen[1])
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(0,screen[0],0,screen[1],0.1,10000.0)
    glMatrixMode(GL_MODELVIEW)
    return pyglet.event.EVENT_HANDLED

@window.event
def on_draw():
    glClearColor(0.5,0.7,0.9,0.0)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glLoadIdentity()
    glTranslatef(0, 0, -1)
    pyglet.app.current.draw()
    fps_display.draw()
    return pyglet.event.EVENT_HANDLED

def show_game(tmp):
    pyglet.app.current = pyglet.app.game_ui
    pyglet.app.current.show()
    pyglet.app.main_menu.hide()

def show_main(tmp):
    pyglet.app.current = pyglet.app.main_menu
    pyglet.app.current.show()
    pyglet.app.game_ui.hide()
    pyglet.app.planner_ui.hide()
    pyglet.app.editor_ui.hide()
    
def show_editor(tmp):
    pyglet.app.current = pyglet.app.editor_ui
    pyglet.app.current.show()
    pyglet.app.main_menu.hide()
    
def show_planner(tmp):
    pyglet.app.current = pyglet.app.planner_ui
    pyglet.app.current.show()
    pyglet.app.main_menu.hide()

            
def setup():
  glgui2.signal = glgui2.Signal(window)  
  pyglet.app.main_menu = MainMenu.guiMainMenu()
  pyglet.app.game_ui = Game.guiGame()
  pyglet.app.editor_ui = Editor.EditorMenu()
  pyglet.app.planner_ui = Plan.guiPlanner()
  
  pyglet.app.editor_ui.hide()
  pyglet.app.game_ui.hide()
  pyglet.app.planner_ui.hide()
  
  pyglet.app.current = pyglet.app.main_menu
  glgui2.common_signals.connect_to_channel('gui_events/on_click/main/start',show_game)
  glgui2.common_signals.connect_to_channel('gui_events/on_click/main/editor',show_editor)
  glgui2.common_signals.connect_to_channel('gui_events/on_click/main/planner',show_planner)
  glgui2.common_signals.connect_to_channel('gui_events/on_click/game/menu/quit',show_main)
  glgui2.common_signals.connect_to_channel('gui_events/on_click/planner/menu/quit',show_main)
  glgui2.common_signals.connect_to_channel('gui_events/on_click/editor/menu/quit',show_main)
  
setup()
fps_display = pyglet.clock.ClockDisplay()
pyglet.app.run()

